package com.zp.controller;

import com.zp.domain.common.Result;
import com.zp.domain.entity.Space;
import com.zp.domain.entity.User;
import com.zp.domain.request.space.analyze.*;
import com.zp.domain.response.*;
import com.zp.enums.ErrorCode;
import com.zp.service.SpaceAnalyzeService;
import com.zp.service.UserService;
import com.zp.utils.ThrowUtils;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/space/analyze")
@Api(tags = "空间分析模块")
@RequiredArgsConstructor
public class SpaceAnalyzeController {
    private final UserService userService;
    private final SpaceAnalyzeService spaceAnalyzeService;

    /**
     * 空间使用大小分析
     *
     * @param analyzeRequest 请求参数
     * @param request        当前登录用户
     * @return 空间使用大小分析结果
     */
    @PostMapping("/usage")
    public Result<SpaceUsageAnalyzeResponse> getSpaceUsageAnalyze(@RequestBody SpaceUsageAnalyzeRequest analyzeRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(analyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        SpaceUsageAnalyzeResponse response = spaceAnalyzeService.getSpaceUsageAnalyze(analyzeRequest, loginUser);
        return Result.success(response);
    }

    /**
     * 空间图片分类分析
     *
     * @param analyzeRequest 分类分析请求
     * @param request        当前登录用户
     * @return 分类分析结果
     */
    @PostMapping("/category")
    public Result<List<SpaceCategoryAnalyzeResponse>> getSpaceCategoryAnalyze(@RequestBody SpaceCategoryAnalyzeRequest analyzeRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(analyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<SpaceCategoryAnalyzeResponse> resultList = spaceAnalyzeService.getSpaceCategoryAnalyze(analyzeRequest, loginUser);
        return Result.success(resultList);
    }

    /**
     * 获取空间图片标签分析
     *
     * @param analyzeRequest 标签分析请求
     * @param request        当前登录用户
     * @return 标签分析结果
     */
    @PostMapping("/tag")
    public Result<List<SpaceTagAnalyzeResponse>> getSpaceTagAnalyze(@RequestBody SpaceTagAnalyzeRequest analyzeRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(analyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<SpaceTagAnalyzeResponse> resultList = spaceAnalyzeService.getSpaceTagAnalyze(analyzeRequest, loginUser);
        return Result.success(resultList);
    }

    /**
     * 空间内图片大小分析
     *
     * @param analyzeRequest 图片大小分析请求
     * @param request        当前登录用户
     * @return 图片大小分析结果
     */
    @PostMapping("/size")
    public Result<List<SpaceSizeAnalyzeResponse>> getSpaceSizeAnalyze(@RequestBody SpaceSizeAnalyzeRequest analyzeRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(analyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<SpaceSizeAnalyzeResponse> resultList = spaceAnalyzeService.getSpaceSizeAnalyze(analyzeRequest, loginUser);
        return Result.success(resultList);
    }

    /**
     * 用户上传行为分析
     *
     * @param analyzeRequest 用户上传行为分析请求
     * @param request        当前登录用户
     * @return 用户上传行为分析结果
     */
    @PostMapping("/user")
    public Result<List<SpaceUserAnalyzeResponse>> getSpaceUserAnalyze(@RequestBody SpaceUserAnalyzeRequest analyzeRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(analyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<SpaceUserAnalyzeResponse> resultList = spaceAnalyzeService.getSpaceUserAnalyze(analyzeRequest, loginUser);
        return Result.success(resultList);
    }

    /**
     * 空间使用排行分析
     *
     * @param analyzeRequest 排行分析请求
     * @param request        当前登录用户
     * @return 排行分析结果
     */
    @PostMapping("/rank")
    public Result<List<Space>> getSpaceRankAnalyze(@RequestBody SpaceRankAnalyzeRequest analyzeRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(analyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<Space> resultList = spaceAnalyzeService.getSpaceRankAnalyze(analyzeRequest, loginUser);
        return Result.success(resultList);
    }


}
